<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');


//添加公共块
if($method == 'addcommon'){
	$name = $_GET['name'];
	$sn = $_GET['sn'];
	$type = to_int($_GET['type']);
	$pos = to_int($_GET['pos']);
	if(isLetterDigitalChinese($name,1,30) && isw($sn,1,30) && $type > 0 && $pos>0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$channel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,$pos,0));
		try{
			$cid=0;
			if(!isset($channel) || count($channel)==0){
				$values = array();
				if($pos===1){
					$values['channel_name'] = '共用头部';
				}else if($pos===2){
					$values['channel_name'] = '共用尾部';
				}
				$values['channel_type'] = 2;
				$values['channel_pos'] = $pos;
				$channelTable->save($values);
				$cid = $db->getDb()->lastInsertId();
			}else{
				$cid = $channel['channel_id'];
			}
			$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
			$blockdb = $blockTable->getOne("block_sn=?", array(trim($sn)));
			if(isset($blockdb) && count($blockdb)>0){
				//唯一码已存在
				echo '{code:2}';
			}else{
				$values=array();
				$values['block_name'] = trim($name);
				$values['block_sn'] = trim($sn);
				$values['block_type'] = $type;
				$values['channel_id'] = $cid;
				$blockTable->save($values);
				echo '{code:1,id:'.$db->getDb()->lastInsertId().'}';
			}
			$db->commit();
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}



//添加首页
if($method == 'addhome'){
	$name = $_GET['name'];
	$sn = $_GET['sn'];
	$type = to_int($_GET['type']);
	if(isLetterDigitalChinese($name,1,30) && isw($sn,1,30) && $type > 0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$channel = $channelTable->getOne("channel_type=? and is_delete=?", array(1,0));
		try{
			$cid=0;
			if(!isset($channel) || count($channel)==0){
				$values = array();
				$values['channel_name'] = '首页';
				$values['channel_type'] = 1;
				$values['channel_pos'] = 0;
				$channelTable->save($values);
				$cid = $db->getDb()->lastInsertId();
			}else{
				$cid = $channel['channel_id'];
			}
			$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
			$blockdb = $blockTable->getOne("block_sn=?", array(trim($sn)));
			if(isset($blockdb) && count($blockdb)>0){
				//唯一码已存在
				echo '{code:2}';
			}else{
				$values=array();
				$values['block_name'] = trim($name);
				$values['block_sn'] = trim($sn);
				$values['block_type'] = $type;
				$values['channel_id'] = $cid;
				$blockTable->save($values);
				echo '{code:1,id:'.$db->getDb()->lastInsertId().'}';
			}
			$db->commit();
		}catch(Exception $e){
			$db->rollback();
			echo '{code:0}';
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

//删除块（并且删除子类）
if($method == 'delblock'){
	$id = to_int($_GET['id']);
	if($id>0){
		$db = new DB();
		$db->beginTransaction();
		$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
		$block = $blockTable->getById($id);
		if(isset($block) && count($block) > 0){
			$tablename = constant('STORE_TABLE_'.$block['block_type']);
			$storeTable = new BaseDao($db->getDb(), $tablename,'id');
			try{
				$blockTable->delete('block_id=?', array($block['block_id']));
				$storeTable->delete('block_id=?', array($block['block_id']));
				$db->commit();
				echo 1;
			}catch(Exception $e){
				$db->rollback();
				echo 0;
			}
		}else{
			echo 0;
		}
		$db->close();
	}else{
		echo 0;
	}
	exit;
}

if($method == 'editnameblock'){
	$name = $_GET['name'];
	$id = to_int($_GET['id']);
	if($id>0 && isLetterDigitalChinese($name,1,30)){
		$db = new DB();
		$db->beginTransaction();
		$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
		try{
			$blockTable->update(array('block_name'=>$name),'block_id=?', array($id));
			$db->commit();
			echo 1;
		}catch(Exception $e){
			$db->rollback();
			echo 0;
		}
		$db->close();
	}else{
		echo 0;
	}
	exit;
}

if($method == 'blocklist'){
	$channelid = to_int($_GET['channelid']);
	$db = new DB();
	$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
	$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
	$blocks = $blockTable->getAll("channel_id=?", array($channelid));
	$channel = $channelTable->getOne("channel_id=? and is_delete=?", array($channelid,0));
	
	$smarty->assign("blocks",$blocks);
	$smarty->assign("channel",$channel);
	$smarty->display("cms/block_list.htm");
	exit;
	
}

//添加首页
if($method == 'addblock'){
	$name = $_GET['name'];
	$sn = $_GET['sn'];
	$type = to_int($_GET['type']);
	$channelid = to_int($_GET['channelid']);
	if($channelid>0 && isLetterDigitalChinese($name,1,30) && isw($sn,1,30) && $type > 0){
		$db = new DB();
		$db->beginTransaction();
		$blockTable = new BaseDao($db->getDb(), 'cms_block','block_id');
		$blockdb = $blockTable->getOne("block_sn=?", array(trim($sn)));
		if(isset($blockdb) && count($blockdb)>0){
			//唯一码已存在
			echo '{code:2}';
		}else{
			try{
				$values=array();
				$values['block_name'] = trim($name);
				$values['block_sn'] = trim($sn);
				$values['block_type'] = $type;
				$values['channel_id'] = $channelid;
				$blockTable->save($values);
				echo '{code:1,id:'.$db->getDb()->lastInsertId().'}';
				$db->commit();
			}catch(Exception $e){
				$db->rollback();
				echo '{code:0}';
			}
		}
		$db->close();
	}else{
		echo '{code:0}';
	}
	exit;
}

